//
//  MusicPlayViewController.m
//  项目
//
//  Created by 张诗雨 on 14/12/31.
//  Copyright (c) 2014年 张诗雨. All rights reserved.
//

#import "MusicPlayViewController.h"
#import "UIImageView+WebCache.h"
#import "WebColor.h"
#import "UIImage+GaussianBlur.h"
#import "MusicListTableViewCell.h"
#import "PlayInfoModel.h"
#import "Singleton.h"
#import "OnedayModel.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface MusicPlayViewController ()
@property (readwrite) AudioStreamerState state;
@end
@implementation MusicPlayViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.dic = [NSDictionary dictionary];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatBack];
    [self createView];
    [self createButton];
    [self searchMusicIndex];
    [self startListening];
    [self addAnimation];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [self playMusic];
}
- (void)createView
{
    // ----滑动的scrollView 控制音乐播放, 歌词, 歌单界面---
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 200)];
    self.scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 3, 0);
    _scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    _scrollView.showsHorizontalScrollIndicator =  NO;
    _scrollView.pagingEnabled = YES;
    [self.view addSubview:_scrollView];
    [_scrollView release];
    
    ///  ---音乐播放页面---
    //旋转的圆图
    self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width + 20, (_scrollView.frame.size.height - (_scrollView.frame.size.width - 40)) / 2, _scrollView.frame.size.width - 40, _scrollView.frame.size.width - 40)];
    [ _imgView setImageWithURL:[NSURL URLWithString:[self.dic objectForKey:@"imgUrl"]]];
    _imgView.layer.cornerRadius = (self.view.frame.size.width - 40) / 2;
    _imgView.clipsToBounds = YES;
    _imgView.userInteractionEnabled = YES;
    [_scrollView addSubview:_imgView];
    [_imgView release];
    
    // 主播名字的lable
    UILabel *BoLable = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width + 20, _scrollView.frame.size.width + 60, 20, 30)];
    BoLable.textColor = [WebColor whiteSmoke];
    BoLable.font = [UIFont systemFontOfSize:15.0f];
    BoLable.text = @"播:";
    [_scrollView addSubview:BoLable];
    [BoLable release];
    self.anchorLable = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width + 40, _scrollView.frame.size.width + 60, 90, 30)];
    self.anchorLable.textColor = [WebColor lightSkyBlue];
    self.anchorLable.text = [[self.dic objectForKey:@"userinfo"] objectForKey:@"uname"];
    self.anchorLable.font = [UIFont systemFontOfSize:15.0f];
    [self.scrollView addSubview:self.anchorLable];
    [_anchorLable release];
    
    // 文章作者的lable
    UILabel *WenLable = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width + 135, _scrollView.frame.size.width + 60, 20, 30)];
    WenLable.textColor = [WebColor whiteSmoke];
    WenLable.font = [UIFont systemFontOfSize:15.0f];
    WenLable.text = @"文:";
    [_scrollView addSubview:WenLable];
    [WenLable release];
    self.ahouthor = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width + 155, _scrollView.frame.size.width + 60, 90, 30)];
    self.ahouthor.textColor = [WebColor lightSkyBlue];
    self.ahouthor.text = [[self.dic objectForKey:@"authorinfo"] objectForKey:@"uname"];
    self.ahouthor.font = [UIFont systemFontOfSize:15.0f];
    [self.scrollView addSubview:self.ahouthor];
    [_ahouthor release];

    // 添加到旋转播放圆图
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, self.view.frame.size.width - 40)];
    img.image = [UIImage imageNamed:@"clock.png"];
    img.alpha = 0.3;
    [self.imgView addSubview:img];
    [img release];
    
    /// 赞的button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(self.view.frame.size.width - 100, _scrollView.frame.size.width + 60, 40, 40);
    [button setBackgroundImage:[UIImage imageNamed:@"iconfont-zan.png"] forState:UIControlStateNormal];
    
    /// 歌单页面
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, _scrollView.frame.size.height)];
    tableView.backgroundColor = [UIColor clearColor];
    tableView.separatorColor=[WebColor whiteSmoke];    //分割cell线颜色
    [tableView registerClass:[MusicListTableViewCell class] forCellReuseIdentifier:@"cell"];
    tableView.delegate = self;
    tableView.dataSource = self;
    [_scrollView addSubview:tableView];
    [tableView release];
    
    /// 歌词界面
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 2, 0, self.view.frame.size.width, self.scrollView.frame.size.height)];
    _webView.backgroundColor = [UIColor clearColor];
    NSString *urlStr = [NSString stringWithFormat:@"%@?style=newting&inside=1",[self.dic objectForKey:@"webview_url"]];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *requset = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:requset];
    _webView.clearsContextBeforeDrawing = YES;
    [_webView setOpaque:NO];//使网页透明
    [self.scrollView addSubview:_webView];
    [_webView release];
    
    //  pageControl 页面切换控制
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.scrollView.frame.size.height + 50, self.view.frame.size.width, 40)];
    [self.view addSubview:_pageControl];
    self.pageControl.numberOfPages = 3; //设置个数
    self.pageControl.currentPage = 1;  //默认位置
    [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    [_pageControl release];
    
    
    // 给背景添加透明度为0.4的黑色背景
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 100, self.view.frame.size.width, 100)];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.4;
    [self.view addSubview:view];
    [view release];
    
    
}
- (void)createButton
{
    /// 播放按钮
    self.playButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _playButton.frame = CGRectMake(self.view.frame.size.width / 2 - 20 ,self.view.frame.size.height - 70, 60, 60);
    [_playButton addTarget:self action:@selector(playMusicAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_playButton];
    Singleton * share = [Singleton shareInstance];
    // 此歌曲正在播放或暂停
    if ([share.musicId isEqualToString:_mid]) {
        if (share.audioPlayer.state == AS_PLAYING) {
            NSLog(@"playing");
            self.musicPlayState = MusicPlayStateStart;
            [_playButton setBackgroundImage:[UIImage imageNamed:@"iconfont-zanting.png"] forState:UIControlStateNormal];
            [self playAnimation];
            
        }
        else if (share.audioPlayer.state == AS_STOPPED) {
            NSLog(@"stopped");
            self.musicPlayState = MusicPlayStatePause;
            [_playButton setBackgroundImage:[UIImage imageNamed:@"iconfont-dianjibofang.png"] forState:UIControlStateNormal];
        }
    }
    // 未有歌曲播放
    else if ([share.musicId isEqualToString:@"-1"]) {
        self.musicPlayState = MusicPlayStateNone;
        [_playButton setBackgroundImage:[UIImage imageNamed:@"iconfont-dianjibofang.png"] forState:UIControlStateNormal];
    }
    // 其他歌曲在播放
    else {
        self.musicPlayState = MusicPlayStateOtherPlaying;
        [_playButton setBackgroundImage:[UIImage imageNamed:@"iconfont-dianjibofang.png"] forState:UIControlStateNormal];
    }
    // 控制播放进度的控制条
    self.progressSlider = [[UISlider alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 100, self.view.frame.size.width, 10)];
    
    _progressSlider.minimumTrackTintColor = [WebColor steelBlue];
    [_progressSlider addTarget:self action:@selector(progressSliderAction:) forControlEvents:UIControlEventValueChanged];
    [_progressSlider addTarget:self action:@selector(progressSliderUp:) forControlEvents:UIControlEventTouchUpInside];
    [_progressSlider addTarget:self action:@selector(progressSliderDown:) forControlEvents:UIControlEventTouchDown];
    [_progressSlider setThumbImage:[UIImage imageNamed:@"slider.png"] forState:UIControlStateNormal];
    [self.view addSubview:self.progressSlider];
    [_progressSlider release];
    /// 上一首按钮
    UIButton *lastButton = [UIButton buttonWithType:UIButtonTypeSystem];
    lastButton.frame = CGRectMake(self.view.frame.size.width / 7 ,self.view.frame.size.height - 65, 40, 40);
    [lastButton setBackgroundImage:[UIImage imageNamed:@"iconfont-shangyishou.png"] forState:UIControlStateNormal];
    [lastButton addTarget:self action:@selector(lastButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:lastButton];
    
    /// 下一首按钮
    UIButton *lowButton = [UIButton buttonWithType:UIButtonTypeSystem];
    lowButton.frame = CGRectMake(self.view.frame.size.width * 6 / 7 - 30 ,self.view.frame.size.height - 65, 40, 40);
    [lowButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xiayishou.png"] forState:UIControlStateNormal];
    [lowButton addTarget:self action:@selector(lowButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:lowButton];
    
    /// 返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(10, 20, 32, 32);
    [backButton setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
}
// 播放按钮触发的点击事件   音乐开始播放
- (void)playMusicAction:(id)sender
{
    [self playMusic];
}
// 返回按钮触发的点击事件  模态回上一界面
- (void)backButtonAction:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
// pageControl控制页面切换方法
- (void)pageAction:(id)sender
{
    [self.scrollView setContentOffset:CGPointMake(_pageControl.currentPage * self.scrollView.frame.size.width, 0) animated:YES];
}
// tableView  返回cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableArray count];
}
// tableView 自定义cell的显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
    MusicListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.backgroundColor = [UIColor clearColor];
    cell.titleLable.text = [dic objectForKey:@"title"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.authorLable.text = [NSString stringWithFormat:@"by:%@",[[dic objectForKey:@"authorinfo"] objectForKey:@"uname"]];
    return cell;
}
/// 旋转动画
- (void)playAnimation
{
    self.imgView.layer.speed = 0.2;
    self.imgView.layer.beginTime = 0.0;
    CFTimeInterval pausedTime = [self.imgView.layer timeOffset];
    CFTimeInterval timeSincePause = [self.imgView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    self.imgView.layer.beginTime = timeSincePause;
    
}
// cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Singleton *share = [Singleton shareInstance];
    NSMutableDictionary * playDic = [_tableArray objectAtIndex:indexPath.row];
    _mid = [playDic objectForKey:@"tingid"];
    _dic = playDic;
    share.playingIndex = indexPath.row;
//    _playingIndex = indexPath.row; //标记当前播放的音乐位置
    [self changeData];
    
}
- (void)changeData
{
    UIImage *image = [UIImage imageWithBlurImage:[NSURL URLWithString: [self.dic objectForKey:@"imgUrl"]] intputRadius:10.0f]; //改变模糊背景大图为当前播放歌曲的图
    _blurredImg.image = image;
    [self.imgView setImageWithURL:[NSURL URLWithString:[self.dic objectForKey:@"imgUrl"]]]; // 改变旋转图片图为当前播放歌曲的图片
    NSString *urlStr = [NSString stringWithFormat:@"%@?style=newting&inside=1",[self.dic objectForKey:@"webview_url"]];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *requset = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:requset];
    self.ahouthor.text = [[self.dic objectForKey:@"authorinfo"] objectForKey:@"uname"]; //改变文章作者为当前播放音乐的文章作者
    self.anchorLable.text = [[self.dic objectForKey:@"userinfo"] objectForKey:@"uname"]; // 改变主播为当前音乐的主播名
    self.titleLable.text = [self.dic objectForKey:@"title"]; //改变曲目为当前播放曲目
    _musicPlayState = MusicPlayStateOtherPlaying; //播放状态是其他音乐正在播放
    [self playMusic];
}
//  cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)creatBack
{
    self.view.backgroundColor = [UIColor whiteColor];
    // 模糊背景大图
    self.blurredImg = [[UIImageView alloc] initWithFrame:self.view.frame];
    _blurredImg.backgroundColor = [WebColor darkGray];
    UIImage *image = [UIImage imageWithBlurImage:[NSURL URLWithString: [self.dic objectForKey:@"imgUrl"]] intputRadius:15.0f];
    _blurredImg.userInteractionEnabled = YES;
    _blurredImg.image = image;
    [self.view addSubview:_blurredImg];
    [_blurredImg release];
    
    //覆盖0.3透明度的view
    self.backView = [[UIView alloc] initWithFrame:self.view.frame];
    self.backView.backgroundColor = [UIColor blackColor];
    self.backView.alpha = 0.3;
    [_blurredImg addSubview:self.backView];
    [self.backView release];
    
    // 歌曲题目
    self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(53, 20, self.view.frame.size.width - 53, 32)];
    self.titleLable.textColor = [WebColor whiteSmoke];
    self.titleLable.textAlignment = NSTextAlignmentCenter;
    self.titleLable.text = [self.dic objectForKey:@"title"];
    [self.view addSubview:self.titleLable];
    [self.titleLable release];
}

#warning 添加的方法
// 查询歌曲在数组中位置
- (void)searchMusicIndex
{
    int count = (int)[_tableArray count];
    for (int i = 0; i < count; i ++) {
        NSMutableDictionary * element = [_tableArray objectAtIndex:i];
        if ([_mid isEqualToString:[element objectForKey:@"tingid"]]) {
            Singleton *share = [Singleton shareInstance];
            share.playingIndex = i;
//            _playingIndex = i;
            break;
        }
    }
}

// 上一首按钮触发事件
- (void)lastButtonAction:(id)sender
{
    Singleton *share = [Singleton shareInstance];
    // 当 当前播放歌曲为第一首时. 提示...
    if (share.playingIndex < 1) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"MineMine" message:@"已经没有上一首了" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alertView show];
        [alertView release];
        
    } else {
        share.playingIndex --;
//        _playingIndex--; //播放歌曲为上一首
        NSMutableDictionary * playDic = [_tableArray objectAtIndex:share.playingIndex];
        _mid = [playDic objectForKey:@"tingid"];
        _dic = playDic;
        //切换所有页面内容
        [self changeData];
        if (_musicPlayState == MusicPlayStateStart) {
            _musicPlayState = MusicPlayStateOtherPlaying;
        }
        else if (_musicPlayState == MusicPlayStateOtherPlaying)
        {
            _musicPlayState = MusicPlayStateStart;
        }
        else if (_musicPlayState == MusicPlayStatePause) {
            _musicPlayState = MusicPlayStateOtherPlaying;
            return;
        }
        else {
            return;
        }
        [self playMusic]; // 音乐播放
    }
}
// 下一首点击事件触发方法
- (void)lowButtonAction:(id)sender
{
    Singleton *share = [Singleton shareInstance];
    int count = (int)[_tableArray count]; //获取歌曲个数
    if (share.playingIndex == count - 1) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"MineMine" message:@"已经没有下一首了" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alertView show];
        [alertView release];
    }
    else {
        share.playingIndex ++;
//        _playingIndex++;
        NSMutableDictionary * playDic = [_tableArray objectAtIndex:share.playingIndex];
        _mid = [playDic objectForKey:@"tingid"];
        _dic = playDic;
        [self changeData];
        // 改变音乐播放状态
        if (_musicPlayState == MusicPlayStateStart) {
            _musicPlayState = MusicPlayStateOtherPlaying;
        }
        else if (_musicPlayState == MusicPlayStateOtherPlaying)
        {
            _musicPlayState = MusicPlayStateStart;
        }
        else if (_musicPlayState == MusicPlayStatePause) {
            _musicPlayState = MusicPlayStateOtherPlaying;
            return;
        }
        else {
            return;
        }
        [self playMusic];
    }
}
// 音乐播放单例
- (void)playMusic
{
    Singleton * share = [Singleton shareInstance];
    switch (_musicPlayState) {
        case MusicPlayStateStart:
        {
            [share.audioPlayer pause];
            [_playButton setBackgroundImage:[UIImage imageNamed:@"iconfont-dianjibofang.png"] forState:UIControlStateNormal];
            _musicPlayState = MusicPlayStatePause;
            [self stopAnimation];
        }
            break;
        case MusicPlayStatePause:
        {
            [share.audioPlayer pause];
            [_playButton setBackgroundImage:[UIImage imageNamed:@"iconfont-zanting.png"] forState:UIControlStateNormal];
            [self playAnimation];
            _musicPlayState = MusicPlayStateStart;
        }
            break;
        case MusicPlayStateNone:
        {
            share.audioPlayer = [[AudioStreamer alloc] initWithURL:[NSURL URLWithString:[self.dic objectForKey:@"musicUrl"]]];
            [share.audioPlayer start];
            [_playButton setBackgroundImage:[UIImage imageNamed:@"iconfont-zanting.png"] forState:UIControlStateNormal];
            [self playAnimation];
            _musicPlayState = MusicPlayStateStart;
            share.musicId = _mid;
            [self createTimers:YES];
        }
            break;
        case MusicPlayStateOtherPlaying:
        {
            [share.audioPlayer stop];
            [share.audioPlayer release];
            share.audioPlayer = [[AudioStreamer alloc] initWithURL:[NSURL URLWithString:[self.dic objectForKey:@"musicUrl"]]];
            [share.audioPlayer start];
            [_playButton setBackgroundImage:[UIImage imageNamed:@"iconfont-zanting.png"] forState:UIControlStateNormal];
            [self playAnimation];
            _musicPlayState = MusicPlayStateStart;
            share.musicId = _mid;
            [self createTimers:YES];
        }
            break;
        default:
            break;
    }
}
// 创建定时器 监听播放时间
-(void)createTimers:(BOOL)create
{
    if (create) {
        {
            [self createTimers:NO];
            // 每0.1秒触发方法
            self.progressUpdateTimer =
            [NSTimer
             scheduledTimerWithTimeInterval:0.1
             target:self
             selector:@selector(updateProgress:)
             userInfo:nil
             repeats:YES];
        }
    }
    else {
        if (_progressUpdateTimer)
        {
            [_progressUpdateTimer invalidate];
            _progressUpdateTimer = nil;
        }
    }
}

- (void)updateProgress:(NSTimer *)updatedTimer
{
    Singleton * share = [Singleton shareInstance];
    if (share.audioPlayer.bitRate!= 0.0)
    {
        // 当前进度
        double progress = share.audioPlayer.progress;
        // 歌曲总长
        double duration = share.audioPlayer.duration;
        if (duration > 0)
        {
            [_progressSlider setEnabled:YES];
            [_progressSlider setValue:progress / duration];
        }
        else
        {
            [_progressSlider setEnabled:NO];
        }
    }
}

- (void)progressSliderAction:(UISlider *)sender {
    Singleton * share = [Singleton shareInstance];
    double duration = share.audioPlayer.duration;
    double progress = sender.value;
    progress *= duration;
    [share.audioPlayer seekToTime:progress];
}
- (void)progressSliderDown:(UISlider *)sender {
    [_progressUpdateTimer invalidate]; //内存管理
}
- (void)progressSliderUp:(UISlider *)sender {
    Singleton * share = [Singleton shareInstance];
    double duration = share.audioPlayer.duration;
    double progress = sender.value;
    progress *= duration;
    [share.audioPlayer seekToTime:progress];
    [self createTimers:YES];
    [_progressUpdateTimer fire];
}
- (void)startListening
{
    // 监听歌曲是播放完成
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    
    [nc addObserver:self selector:@selector(stateChange:) name:@"ASStatusChangedNotification" object:nil];
}
///  改变状态
- (void)stateChange:(NSNotification *)nc
{
    //    NSLog(@"%@", [nc.userInfo objectForKey:@"state"]);
    if ([[nc.userInfo objectForKey:@"state"] isEqualToString:@"8"]) {
        NSLog(@"播放结束");
        Singleton *share = [Singleton shareInstance];
        _musicPlayState = MusicPlayStateOtherPlaying;
        share.playingIndex++;
//        _playingIndex++;
        NSMutableDictionary * playDic = [_tableArray objectAtIndex:share.playingIndex];
        _mid = [playDic objectForKey:@"tingid"];
        _dic = playDic;
        [self changeData];
    }
}

///  动画停止
- (void)stopAnimation
{
    CFTimeInterval pausedTime = [self.imgView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    self.imgView.layer.speed = 0.0;
    self.imgView.layer.timeOffset = pausedTime;
}
/// 添加动画
- (void)addAnimation
{
    CABasicAnimation *monkeyAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    monkeyAnimation.toValue = [NSNumber numberWithFloat:2.0 *M_PI];
    monkeyAnimation.duration = 6.0f;
    monkeyAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    monkeyAnimation.cumulative = NO;
    monkeyAnimation.removedOnCompletion = NO; //No Remove
    
    monkeyAnimation.repeatCount = FLT_MAX;
    [self.imgView.layer addAnimation:monkeyAnimation forKey:@"AnimatedKey"];
    [self.imgView stopAnimating];
    self.imgView.layer.speed = 0.0;
}
// scrollView视图滑动改变PageControl的位置
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int halfX = _scrollView.frame.size.width / 2;
    _pageControl.currentPage = (_scrollView.contentOffset.x - (self.scrollView.bounds.size.width - halfX)) / (_scrollView.frame.size.width) + 1;
}
- (void)dealloc
{
    [_mid release];
    [_playButton release];
    [_pageControl release];
    [_dic release];
    [_imgView release];
    [_backView release];
    [_tableArray release];
    [_ahouthor release];
    [_anchorLable release];
    [_scrollView release];
    [_titleLable release];
    [_blurredImg release];
    [super dealloc];
}

@end
